package com.hfjyz.base.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.hfjyz.base.constant.BaseConstant;

public class BaseUtil {

    public static boolean isAlpha(String value) {
        if (value == null) {
            return false;
        }
        for (int i = 0; i < value.length(); i++) {
            char charData = value.charAt(i);
            if (((charData < 'A') || (charData > 'Z')) && ((charData < 'a') || (charData > 'z'))
                    && ((charData < '0') || (charData > '9'))) {
                return false;
            }
        }
        return true;
    }

    public static Set<Integer> getRandomIndex(int baseCount, int randomCount) {
        Set<Integer> result = new HashSet<Integer>();
        if (baseCount <= randomCount) {
            for (int i = 0; i < baseCount; i++) {
                result.add(i);
            }
            return result;
        }
        while (result.size() < randomCount) {
            int index = (int) (Math.random() * baseCount);
            result.add(index);
        }

        return result;
    }

    public static boolean isDigital(String str) {
        return str == null || "".equals(str) ? false : str.matches("^[0-9]*$");
    }
    
    public static boolean checkMail(String mailAddress) {
        boolean tag = true;
        final String pattern1 = "^([a-z0-9A-Z]+[_-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        final Pattern pattern = Pattern.compile(pattern1);
        final Matcher mat = pattern.matcher(mailAddress);
        if (!mat.find()) {
            tag = false;
        }
        return tag;
    }

    public static boolean checkPhone(String phonenumber) {
        String phoneFull = "0\\d{2,3}-\\d{7,8}";
        String phone = "\\d{7,8}";
        String phoneWithSubNo = "0\\d{2,3}-\\d{7,8}-\\d+";
        Pattern p1 = Pattern.compile(phone);
        Matcher m1 = p1.matcher(phonenumber);

        Pattern p2 = Pattern.compile(phoneFull);
        Matcher m2 = p2.matcher(phonenumber);
        
        Pattern p3 = Pattern.compile(phoneWithSubNo);
        Matcher m3 = p3.matcher(phonenumber);

        return m1.matches() || m2.matches() || m3.matches();
    }

    public static boolean checkCellPhone(String mobiles) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }
    
    public static boolean checkQQ(String qq) {
        return isDigital(qq);
    }
    
    public static boolean checkMSN(String msn) {
        return checkMail(msn);
    }
    
    public static boolean isEmpty(String obj) {
        if (obj == null) {
            return true;
        }
        if (obj.trim().isEmpty()) {
            return true;
        }
        return false;
    }
    
    public static Date parseDate(String dateString) {
        Date dt = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(BaseConstant.DATE_PATTERN_BAR);
            dt = sdf.parse(dateString);
        } catch (Exception e) {
        }
        if (dt == null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(BaseConstant.DATE_PATTERN_SLASH);
                dt = sdf.parse(dateString);
            } catch (Exception e) {
            }
        }
        if (dt == null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(BaseConstant.DATE_PATTERN_CHAR);
                dt = sdf.parse(dateString);
            } catch (Exception e) {
            }
        }

        return dt;
    }

    public static String decodeChangeLine(String txt) {
        txt = txt.replaceAll("\r\n", "<br>");
        txt = txt.replaceAll("\n", "<br>");
        
        return txt;
    }
}
